package com.cmpe451.nutty.service;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import com.cmpe451.nutty.model.User;
import com.cmpe451.nutty.so.GuiUser;

import java.security.*;
import java.util.Objects;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

@Component
public class MailServiceImpl implements MailService, InitializingBean {

	private final String username = "nuttyconfirmation@gmail.com";
	private final String password = "caprisun2014";

	@Override
	public void afterPropertiesSet() throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void sendConfirmationMail(User user, String currentUrl) throws NoSuchAlgorithmException {

		String to = user.getEmail();

		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "587");
		System.out.println("burası: " + currentUrl);
		int index = currentUrl.indexOf("save");
		
		String url = currentUrl.substring(0, index);
		System.out.println("url: " + url);
		Long userId = user.getId();
		String userID = Objects.toString(userId, null);
		String hash = getMd5(user);
		Session session = Session.getInstance(props,
				new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(username, password);
					}
				});

		try {

			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("nuttyconfirmation@gmail.com"));
			message.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(to));
			message.setSubject("Confirmation from Your Recipe Friend NUTTY!");
			message.setText(url + "/activateUser?userid="+userId+ "&hash=" + hash);

			Transport.send(message);

			System.out.println("Done");

		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}

	}

	private String getMd5(User user) throws NoSuchAlgorithmException {

		String username = user.getUsername();
		MessageDigest md = MessageDigest.getInstance("MD5");
		md.update(username.getBytes());
		byte[] digest = md.digest();
		StringBuffer sb = new StringBuffer();
		for (byte b : digest) {
			sb.append(String.format("%02x", b & 0xff));
		}

		return sb.toString();

	}

	@Override
	public void sendRecoveryMail(String user, String email, String pass) {
		// TODO Auto-generated method stub

		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "587");
		Session session = Session.getInstance(props,
				new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(username, password);
					}
				});

		try {

			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("nuttyconfirmation@gmail.com"));
			message.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(email));
			message.setSubject("Did you forgot your e-mail?");
			message.setText("Hello" + user + ";\n Here is your password: " + pass);

			Transport.send(message);

			System.out.println("Done");

		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}

		
		
	}

	@Override
	public void sendDeactivateMail(User user, String currentUrl) throws NoSuchAlgorithmException {
		// TODO Auto-generated method stub
		
		String to = user.getEmail();

		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "587");
		int index = currentUrl.indexOf("deactivate");
		
		String url = currentUrl.substring(0, index);
		Long userId = user.getId();
		String hash = getMd5(user);
		Session session = Session.getInstance(props,
				new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(username, password);
					}
				});

		try {

			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("nuttyconfirmation@gmail.com"));
			message.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(to));
			message.setSubject("Deactivation of Your Nutty Account!");
			message.setText("You deactivated your account. In order to activate your account click: " + url + "/activateUser?userid="+userId+ "&hash=" + hash);

			Transport.send(message);

			System.out.println("Done");

		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
		
		
	}

}
